use pest::Parser;
use pest_derive::Parser;
use std::{env, fs};

#[derive(Parser)]
#[grammar = "lexer.pest"]
pub struct SysyParser;

fn main() {
    // 收集命令行参数
    let args: Vec<String> = env::args().collect();

    // 检查是否提供了文件名
    if args.len() < 2 {
        eprintln!("Usage: {} <filename>", args[0]);
        std::process::exit(1);
    }

    // 获取文件名
    let filename = &args[1];

    let unparsed_file = fs::read_to_string(filename).expect("cannot read file");

    let file = SysyParser::parse(Rule::FILE, &unparsed_file)
        .unwrap()
        .next()
        .unwrap();

    for pair in file.into_inner() {
        let (line, _) = pair.line_col();
        let literal = pair.as_str();
        for token in pair.into_inner() {
            match token.as_rule() {
                Rule::ERR => {
                    eprintln!("Error type {} at Line {}:[errorMessage]", literal, line)
                }
                other => {
                    eprintln!("{:?} {} at Line {}.", other, literal, line);
                }
            }
        }
    }
}
